package com.yuexun.dhdoctor.module.commun.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;

import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.base.AppContext;
import com.yuexun.dhdoctor.base.BaseEntity;
import com.yuexun.dhdoctor.baselistener.OnDataBackListener;
import com.yuexun.dhdoctor.config.AppConfig;
import com.yuexun.dhdoctor.module.Contract.bean.GroupGroupBean;
import com.yuexun.dhdoctor.module.Contract.bean.GroupGroupData;
import com.yuexun.dhdoctor.module.Contract.bean.People;
import com.yuexun.dhdoctor.module.Contract.model.impl.GroupModel;
import com.yuexun.dhdoctor.module.Contract.presenter.impl.GroupPresenter;
import com.yuexun.dhdoctor.module.Contract.ui.view.IGroupView;
import com.yuexun.dhdoctor.module.commun.adapter.GroupExpandableAdapter;
import com.yuexun.dhdoctor.module.commun.ui.Act_Choice_Group;
import com.yuexun.dhdoctor.module.commun.ui.Act_Choice_Member;
import com.yuexun.dhdoctor.widget.EmptyLayout;

import org.kymjs.kjframe.ui.KJFragment;
import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * Created by Administrator on 2017/3/27 0027.
 */

public class Frag_Choice_Group extends KJFragment implements IGroupView {

    @Bind(R.id.group_lit)
    ExpandableListView groupLit;
    @Bind(R.id.empty_layout)
    EmptyLayout emptyLayout;

    private GroupPresenter presenter;
    List<GroupGroupData> publicgroupGroupDataBeans = new ArrayList<>();
    List<GroupGroupData> groupGroupDataBeans = new ArrayList<>();
    List<GroupGroupBean> groupGroupBeen = new ArrayList<>();
    //    private BaseAdapter adapter;
    GroupExpandableAdapter adapter;

    ArrayList<People> usersBeans_origin = new ArrayList<>();
    OnDataBackListener listener;
    String groupType = "2";
    int from;//0多选 1单选
    private int isSelectMe;//0：不能选择  1：能选择自己

    public void setUsersBeans_origin(ArrayList<People> usersBeans_origin, OnDataBackListener listener, int from, int isSelectMe) {
        this.usersBeans_origin = usersBeans_origin;
        this.listener = listener;
        this.from = from;
        this.isSelectMe = isSelectMe;
    }

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
        return null;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frag_choice_group, null);
        ButterKnife.bind(this, view);
        initView();
        return view;
    }

    void initView() {
        adapter = new GroupExpandableAdapter(AppContext.getInstance(), groupGroupBeen);
        groupLit.setAdapter(adapter);
        groupLit.setGroupIndicator(null);
        groupLit.setDivider(null);
        presenter = new GroupPresenter(new GroupModel(), this);

        new Handler().postDelayed(new Runnable() {
            public void run() {
                groupGroupBeen.clear();
                groupType = "2";
                Map<String, String> params = new HashMap<>();
                params.put("sessionUuid", PreferenceHelper.readString(AppContext.getInstance(), AppConfig.USER_INFO, AppConfig.SESSION_ID));
                params.put("groupType", groupType);
                presenter.getGroupDatas(params);
            }
        }, 1000);

        emptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
            }
        });

        groupLit.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {
                Bundle bundle = new Bundle();
                Intent intent = new Intent(AppContext.getInstance(), Act_Choice_Group.class);
                if (from == 0) {
                    bundle.putSerializable("DataBean", groupGroupBeen.get(i).getDatas().get(i1));
                    bundle.putSerializable("usersBeans", ((Act_Choice_Member) getActivity()).usersBeans_origin);
                    bundle.putString("from", from + "");
                    bundle.putInt("isSelectMe", isSelectMe);
                } else {
                    bundle.putSerializable("DataBean", groupGroupBeen.get(i).getDatas().get(i1));
                    bundle.putSerializable("usersBeans", usersBeans_origin);
                    bundle.putString("from", from + "");
                    bundle.putInt("isSelectMe", isSelectMe);
                }
                intent.putExtras(bundle);
                getActivity().startActivityForResult(intent, 2);

                return true;
            }
        });
    }


    @Override
    public void showGroupDatas(BaseEntity<GroupGroupData> baseEntity) {
        if ("200".equals(baseEntity.getStatus())) {
            if (groupType.equals("2")) {
                if (publicgroupGroupDataBeans.size() > 0) {
                    publicgroupGroupDataBeans.clear();
                }
                publicgroupGroupDataBeans = baseEntity.getDatas();
                GroupGroupBean groupBean = new GroupGroupBean();
                groupBean.setGroup_name("公共群组");
                groupBean.setDatas(publicgroupGroupDataBeans);
                groupGroupBeen.add(groupBean);

                groupType = "1";
                Map<String, String> params = new HashMap<>();
                params.put("sessionUuid", PreferenceHelper.readString(AppContext.getInstance(), AppConfig.USER_INFO, AppConfig.SESSION_ID));
                params.put("groupType", groupType);
                presenter.getGroupDatas(params);
            } else {
                if (groupGroupDataBeans.size() > 0) {
                    groupGroupDataBeans.clear();
                }
                groupGroupDataBeans = baseEntity.getDatas();

                GroupGroupBean groupBean = new GroupGroupBean();
                groupBean.setGroup_name("个人群组");
                groupBean.setDatas(groupGroupDataBeans);
                groupGroupBeen.add(groupBean);

                adapter.setGroupGroupBeen(groupGroupBeen);
                if (groupLit != null) {
                    for (int i = 0; i < adapter.getGroupCount(); i++) {
                        groupLit.expandGroup(i);

                    }
                }
            }

        } else {
            ViewInject.toast("数组：" + baseEntity.getMessage());
        }

        if (emptyLayout != null) {
            emptyLayout.dismiss();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

}
